package com.yonghui.summer.ast.basic;

import javax.lang.model.element.Element;
import java.util.HashMap;
import java.util.Optional;

/**
 * @auther yangchuan
 * @date 2020-03-08 18:08
 */
public class DataType {
    private static final HashMap<String, String> types = new HashMap<>();


    static {

        types.put("boolean", "boolean");
        types.put("boolean[]", "boolean");
        types.put("boolean>", "boolean");

        types.put("character", "char");
        types.put("character[]", "char");
        types.put("character>", "char");
        types.put("char[]", "char");
        types.put("char>", "char");

        types.put("byte", "byte");
        types.put("byte[]", "byte");
        types.put("byte>", "byte");

        types.put("short", "short");
        types.put("short[]", "short");
        types.put("short>", "short");

        types.put("integer", "int");
        types.put("integer[]", "int");
        types.put("integer>", "int");
        types.put("int", "int");
        types.put("int[]", "int");
        types.put("int>", "int");

        types.put("float", "float");
        types.put("float[]", "float");
        types.put("float>", "float");

        types.put("double", "double");
        types.put("double[]", "double");
        types.put("double>", "double");

        types.put("long", "long");
        types.put("long[]", "long");
        types.put("long>", "long");

        types.put("string", "string");
        types.put("string[]", "string");
        types.put("string>", "string");

        types.put("date", "date-time");
        types.put("date[]", "date-time");
        types.put("date>", "date-time");

        types.put("bigdecimal", "bigdecimal");
        types.put("bigdecimal[]", "bigdecimal");
        types.put("bigdecimal>", "bigdecimal");


        types.put("biginteger", "biginteger");
        types.put("biginteger[]", "biginteger");
        types.put("biginteger>", "biginteger");


        types.put("currency", "string");
        types.put("currency[]", "string");
        types.put("currency>", "string");


        types.put("multipartfile", "__file");
        types.put("multipartfile[]", "__file");
        types.put("multipartfile>", "__file");
    }

    public static boolean isCollections(String str){
        return str.startsWith("java.util.List") ||
                str.startsWith("java.util.ArrayList") ||
                str.startsWith("java.util.LinkedList") ||
                str.startsWith("java.util.Set") ||
                str.startsWith("java.util.HashSet") ||
                str.startsWith("java.util.TreeSet") ||
                str.startsWith("java.util.SortedSet")||
                str.startsWith("java.util.Vector");
    }

    public static boolean isCollections(Element element){
        return isCollections(element.asType().toString());
    }

    public static String getDataType(String key){
        return Optional.ofNullable(types.get(key)).orElse("");
    }

    public static String getDataType(Element element){

        String str = element.asType().toString();

        if (element.asType().getKind().isPrimitive()) {
            return element.asType().getKind().name();
        } else {
            String temp = str.substring(str.lastIndexOf(".") + 1).toLowerCase();
            return getDataType(temp);
        }

    }

}
